<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 *
 * @package  	PyroCMS
 * @subpackage  Categories
 * @category  	Module
 * @author  	PyroCMS Dev Team
 */
class Admin_Rate extends Admin_Controller {

	/**
	 * The current active section
	 * @access protected
	 * @var int
	 */
	protected $section = 'escort-personal';
	protected $_ci;
	/**
	 * The constructor
	 * @access public
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		$this->_ci = &get_instance();
		$this->lang->load('be_escort');
		$this->lang->load('be_escort_rate');
	}
	
	/**
	 * Index method, lists all categories
	 * @access public
	 * @return void
	 */
	public function index($id)
	{
		$id OR redirect('admin/be_escort');
		
		$escort = $this->_ci->escort_entity->init('id', $id);
		$user = $this->_ci->user_entity->init('id', $escort->getUserId());
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		if(!$lockedEscort){
			$this->_ci->locked_escort_entity->createNewLockedEscortEntity($id);
			$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $id);
		}
		
		if(!$escort || !$user){
			die();
		}
		$this->data['escort'] = $escort;
		$this->data['user'] = $user;
		$this->data['lockedEscort'] = $lockedEscort;
		$this->pyrocache->delete_all('modules_m');
		$this->load->view('admin/rate/index', $this->data);
	}
	public function save(){
		$result = array();
		$result['result'] = true;
		$escortId = $this->input->post('escort_id');
		$escort = $this->_ci->escort_entity->init('id',$escortId);
		$lockedEscort = $this->_ci->locked_escort_entity->init('escort_id', $escortId);
		$user = $this->_ci->user_entity->init('id', $escort->getUserId());
		
		$rates_payment_ids = isset($_POST['rates_payment']) ? implode(',', $_POST['rates_payment']) : '';
		$rates_public_payment = $this->input->post('public_payment_method', TRUE);
		$rates_public_price = $this->input->post('public_pricing_time', TRUE);
		$rates_transport_id = $this->input->post('rates_transport_id', TRUE);
		$rates_public_transport = $this->input->post('public_transport_include', TRUE);
		$rates_extras_ids = isset($_POST['rates_extras']) ? implode(',', $_POST['rates_extras']) : '';
		$rates_public_extras = $this->input->post('public_extras', TRUE);
		$rates_price_time_array = isset($_POST['rates_price_time']) ? ($_POST['rates_price_time']) : array();
		
		$escort->setPaymentMethodIds($rates_payment_ids);
		$escort->setPublicPaymentMethod($rates_public_payment);
		$escort->setPublicPricingTime($rates_public_price);
		$escort->setTransportIncludeId($rates_transport_id);
		$escort->setPublicTransportInclude($rates_public_transport);
		$escort->setExtrasIds($rates_extras_ids);
		$escort->setPublicExtras($rates_public_extras);
		$escort->adminUpdate();
		
		$currentPricingTimeArray = $escort->getPricingTimeIdArray($escortId);
		foreach($currentPricingTimeArray as $id){
			if(! in_array($id, $rates_price_time_array)){
				$this->db->where(array('escort_id'=>$escortId, 'pricing_time_id'=>$id))->delete(TBL_PRICING_TIME);
			}
		}
		
		foreach($rates_price_time_array as $key => $id){
			$price = $this->input->post("rates_price_value_$key", TRUE);
			$escort->setPricingTime($escortId, $id, $price);
		}

		//$lockedEscort = new CI_Locked_escort_entity();
		$lockedEscort->setLockedPaymentMethod($this->input->post('locked_payment_method'));
		$lockedEscort->setLockedPricingTime($this->input->post('locked_pricing_time'));
		$lockedEscort->setLockedTransport($this->input->post('locked_transport'));
		$lockedEscort->setLockedExtras($this->input->post('locked_extras'));
		$lockedEscort->update();
		
		$result['message'] = lang('escort_be_escort_rate_update_success');
		echo json_encode($result);
	}
}